#!/bin/sh
#
# codis-ha        init file for starting up the codis-ha daemon
#
# chkconfig:   - 20 80
# description: Starts and stops the codis-ha daemon.
# author: lework
# Source function library.

. /etc/rc.d/init.d/functions

name="codis-ha"
exec="{{ codis_bin_path }}/codis-ha"
pidfile="{{ codis_pid_path }}codis-ha.pid"
logpath="{{ codis_ha_logs_path }}/ha.log"
loglevel="{{ codis_loglevel }}"
dashboard="{{ codis_dashboard_addr }}"
user="root"


lockfile=/var/lock/subsys/codis-ha

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $name: "
    /sbin/runuser -s /bin/bash $user -c "$exec --log=$logpath --log-level=$loglevel --dashboard=$dashboard & echo '$!' > $pidfile"
    retval=$?
    [ $retval -eq 0 ] && touch $lockfile && success $"$base startup" || failure $"$base startup"
    echo
    return $retval
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

rh_status() {
    status -p $pidfile $name
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && rh_status && exit 0
        $1
        ;;
    stop)
        rh_status_q || rh_status || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac
exit $?

